﻿using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace Gems.Console
{
    public class ColoredConsoleTraceListener : ConsoleTraceListener
    {
        readonly Dictionary<TraceEventType, ConsoleColor> _eventColor = new Dictionary<TraceEventType, ConsoleColor>();

        public ColoredConsoleTraceListener()
        {
            _eventColor.Add(TraceEventType.Verbose, ConsoleColor.DarkGray);
            _eventColor.Add(TraceEventType.Information, ConsoleColor.Gray);
            _eventColor.Add(TraceEventType.Warning, ConsoleColor.Yellow);
            _eventColor.Add(TraceEventType.Error, ConsoleColor.DarkRed);
            _eventColor.Add(TraceEventType.Critical, ConsoleColor.Red);
            _eventColor.Add(TraceEventType.Start, ConsoleColor.DarkCyan);
            _eventColor.Add(TraceEventType.Stop, ConsoleColor.DarkCyan);
        }

        public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message)
        {
            var originalColor = System.Console.ForegroundColor;
            System.Console.ForegroundColor = GetEventColor(eventType, originalColor);
            base.TraceEvent(eventCache, source, eventType, id, message);
            System.Console.ForegroundColor = originalColor;
        }

        public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args)
        {
            var originalColor = System.Console.ForegroundColor;
            System.Console.ForegroundColor = GetEventColor(eventType, originalColor);
            base.TraceEvent(eventCache, source, eventType, id, format, args);
            System.Console.ForegroundColor = originalColor;
        }
        
        private ConsoleColor GetEventColor(TraceEventType eventType, ConsoleColor defaultColor)
        {
            return !_eventColor.ContainsKey(eventType) ? defaultColor : _eventColor[eventType];
        }
    }
}